<!DOCTYPE html>
<html lang="zh" dir="ltr" class="client-nojs">
<head>
<title>语句</title>
<meta charset="UTF-8">
<meta name="generator" content="MediaWiki 1.21.2">
<link rel="shortcut icon" href="../../../common/favicon.ico">
<link rel="stylesheet" href="../../../common/ext.css">
<meta name="ResourceLoaderDynamicStyles" content="">
<link rel="stylesheet" href="../../../common/site_modules.css">
<style>a:lang(ar),a:lang(ckb),a:lang(fa),a:lang(kk-arab),a:lang(mzn),a:lang(ps),a:lang(ur){text-decoration:none}#toc{display:none}.editsection{display:none}
/* cache key: mwiki1-mwiki_zh_:resourceloader:filter:minify-css:7:15cea3ec788a65b5187d4018eed543bf */</style>

<script src="../../../common/startup_scripts.js"></script>
<script>if(window.mw){
mw.config.set({"wgCanonicalNamespace":"","wgCanonicalSpecialPageName":false,"wgNamespaceNumber":0,"wgPageName":"cpp/language/statements","wgTitle":"cpp/language/statements","wgCurRevisionId":72340,"wgArticleId":8559,"wgIsArticle":true,"wgAction":"view","wgUserName":null,"wgUserGroups":["*"],"wgCategories":[],"wgBreakFrames":false,"wgPageContentLanguage":"zh","wgSeparatorTransformTable":["",""],"wgDigitTransformTable":["",""],"wgDefaultDateFormat":"zh","wgMonthNames":["","1月","2月","3月","4月","5月","6月","7月","8月","9月","10月","11月","12月"],"wgMonthNamesShort":["","1月","2月","3月","4月","5月","6月","7月","8月","9月","10月","11月","12月"],"wgRelevantPageName":"cpp/language/statements","wgUserVariant":"zh","wgRestrictionEdit":[],"wgRestrictionMove":[]});
}</script><script>if(window.mw){
mw.loader.implement("user.options",function(){mw.user.options.set({"ccmeonemails":0,"cols":80,"date":"default","diffonly":0,"disablemail":0,"disablesuggest":0,"editfont":"default","editondblclick":0,"editsection":0,"editsectiononrightclick":0,"enotifminoredits":0,"enotifrevealaddr":0,"enotifusertalkpages":1,"enotifwatchlistpages":0,"extendwatchlist":0,"externaldiff":0,"externaleditor":0,"fancysig":0,"forceeditsummary":0,"gender":"unknown","hideminor":0,"hidepatrolled":0,"imagesize":2,"justify":0,"math":1,"minordefault":0,"newpageshidepatrolled":0,"nocache":0,"noconvertlink":0,"norollbackdiff":0,"numberheadings":0,"previewonfirst":0,"previewontop":1,"quickbar":5,"rcdays":7,"rclimit":50,"rememberpassword":0,"rows":25,"searchlimit":20,"showhiddencats":0,"showjumplinks":1,"shownumberswatching":1,"showtoc":0,"showtoolbar":1,"skin":"cppreference2","stubthreshold":0,"thumbsize":2,"underline":2,"uselivepreview":0,"usenewrc":0,"watchcreations":0,"watchdefault":0,"watchdeletion":0,
"watchlistdays":3,"watchlisthideanons":0,"watchlisthidebots":0,"watchlisthideliu":0,"watchlisthideminor":0,"watchlisthideown":0,"watchlisthidepatrolled":0,"watchmoves":0,"wllimit":250,"variant":"zh","language":"zh","searchNs0":true,"searchNs1":false,"searchNs2":false,"searchNs3":false,"searchNs4":false,"searchNs5":false,"searchNs6":false,"searchNs7":false,"searchNs8":false,"searchNs9":false,"searchNs10":false,"searchNs11":false,"searchNs12":false,"searchNs13":false,"searchNs14":false,"searchNs15":false,"gadget-MathJax":1,"gadget-ColiruCompiler":1});;},{},{});mw.loader.implement("user.tokens",function(){mw.user.tokens.set({"editToken":"+\\","patrolToken":false,"watchToken":false});;},{},{});
/* cache key: mwiki1-mwiki_zh_:resourceloader:filter:minify-js:7:258d7cd6aa9aa67dee25e01fb6a9e505 */
}</script>
<script>if(window.mw){
mw.loader.load(["mediawiki.page.startup","mediawiki.legacy.wikibits","mediawiki.legacy.ajax"]);
}</script>
<style type="text/css">/*<![CDATA[*/
.source-cpp {line-height: normal;}
.source-cpp li, .source-cpp pre {
	line-height: normal; border: 0px none white;
}
/**
 * GeSHi Dynamically Generated Stylesheet
 * --------------------------------------
 * Dynamically generated stylesheet for cpp
 * CSS class: source-cpp, CSS id: 
 * GeSHi (C) 2004 - 2007 Nigel McNie, 2007 - 2008 Benny Baumann
 * (http://qbnz.com/highlighter/ and http://geshi.org/)
 * --------------------------------------
 */
.cpp.source-cpp .de1, .cpp.source-cpp .de2 {font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;}
.cpp.source-cpp  {font-family:monospace;}
.cpp.source-cpp .imp {font-weight: bold; color: red;}
.cpp.source-cpp li, .cpp.source-cpp .li1 {font-weight: normal; vertical-align:top;}
.cpp.source-cpp .ln {width:1px;text-align:right;margin:0;padding:0 2px;vertical-align:top;}
.cpp.source-cpp .li2 {font-weight: bold; vertical-align:top;}
.cpp.source-cpp .kw1 {color: #0000dd;}
.cpp.source-cpp .kw2 {color: #0000ff;}
.cpp.source-cpp .kw3 {color: #0000dd;}
.cpp.source-cpp .kw4 {color: #0000ff;}
.cpp.source-cpp .co1 {color: #909090;}
.cpp.source-cpp .co2 {color: #339900;}
.cpp.source-cpp .coMULTI {color: #ff0000; font-style: italic;}
.cpp.source-cpp .es0 {color: #008000; font-weight: bold;}
.cpp.source-cpp .es1 {color: #008000; font-weight: bold;}
.cpp.source-cpp .es2 {color: #008000; font-weight: bold;}
.cpp.source-cpp .es3 {color: #008000; font-weight: bold;}
.cpp.source-cpp .es4 {color: #008000; font-weight: bold;}
.cpp.source-cpp .es5 {color: #008000; font-weight: bold;}
.cpp.source-cpp .br0 {color: #008000;}
.cpp.source-cpp .sy0 {color: #008000;}
.cpp.source-cpp .sy1 {color: #000080;}
.cpp.source-cpp .sy2 {color: #000040;}
.cpp.source-cpp .sy3 {color: #000040;}
.cpp.source-cpp .sy4 {color: #008080;}
.cpp.source-cpp .st0 {color: #008000;}
.cpp.source-cpp .nu0 {color: #000080;}
.cpp.source-cpp .nu6 {color: #000080;}
.cpp.source-cpp .nu8 {color: #000080;}
.cpp.source-cpp .nu12 {color: #000080;}
.cpp.source-cpp .nu16 {color:#000080;}
.cpp.source-cpp .nu17 {color:#000080;}
.cpp.source-cpp .nu18 {color:#000080;}
.cpp.source-cpp .nu19 {color:#000080;}
.cpp.source-cpp .ln-xtra, .cpp.source-cpp li.ln-xtra, .cpp.source-cpp div.ln-xtra {background-color: #ffc;}
.cpp.source-cpp span.xtra { display:block; }

/*]]>*/
</style><!--[if lt IE 7]><style type="text/css">body{behavior:url("/mwiki/skins/cppreference2/csshover.min.htc")}</style><![endif]--></head>
<body class="mediawiki ltr sitedir-ltr ns-0 ns-subject page-cpp_language_statements skin-cppreference2 action-view cpp-navbar">
        <!-- header -->
        <!-- /header -->
        <!-- content -->
<div id="cpp-content-base">
            <div id="content">
                <a id="top"></a>
                <div id="mw-js-message" style="display:none;"></div>
                                <!-- firstHeading -->
<h1 id="firstHeading" class="firstHeading">语句</h1>
                <!-- /firstHeading -->
                <!-- bodyContent -->
                <div id="bodyContent">
                                        <!-- tagline -->
                    <div id="siteSub">来自cppreference.com</div>
                    <!-- /tagline -->
                                        <!-- subtitle -->
                    <div id="contentSub"><span class="subpages">&lt; <a href="../../cpp.html" title="cpp">cpp</a>‎ | <a href="../language.html" title="cpp/language">language</a></span></div>
                    <!-- /subtitle -->
                                                            <!-- bodycontent -->
                    <div id="mw-content-text" lang="zh" dir="ltr" class="mw-content-ltr"><div class="t-navbar" style=""><div class="t-navbar-sep"> </div><div class="t-navbar-head"><a href="../../cpp.html" title="cpp"> C++</a><div class="t-navbar-menu"><div><div><table class="t-nv-begin" cellpadding="0" style="line-height:1.1em">
<tr class="t-nv"><td colspan="5"> <a href="../language.html" title="cpp/language">语言</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../header.html" title="cpp/header">标准库头文件</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../freestanding.html" title="cpp/freestanding"> 自立与有宿主实现</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../named_req.html" title="cpp/named req">具名要求</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../utility.html#.E8.AF.AD.E8.A8.80.E6.94.AF.E6.8C.81" title="cpp/utility">语言支持库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../concepts.html" title="cpp/concepts">概念库</a> <span class="t-mark-rev t-since-cxx20">(C++20)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../error.html" title="cpp/error">诊断库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../utility.html" title="cpp/utility">工具库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../string.html" title="cpp/string">字符串库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../container.html" title="cpp/container">容器库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../iterator.html" title="cpp/iterator">迭代器库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../ranges.html" title="cpp/ranges"> 范围库</a> <span class="t-mark-rev t-since-cxx20">(C++20)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../algorithm.html" title="cpp/algorithm">算法库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../numeric.html" title="cpp/numeric">数值库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../locale.html" title="cpp/locale">本地化库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../io.html" title="cpp/io">输入/输出库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../filesystem.html" title="cpp/filesystem">文件系统库</a> <span class="t-mark-rev t-since-cxx17">(C++17)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../regex.html" title="cpp/regex">正则表达式库</a> <span class="t-mark-rev t-since-cxx11">(C++11)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../atomic.html" title="cpp/atomic">原子操作库</a> <span class="t-mark-rev t-since-cxx11">(C++11)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../thread.html" title="cpp/thread">线程支持库</a> <span class="t-mark-rev t-since-cxx11">(C++11)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../experimental.html" title="cpp/experimental">技术规范</a> </td></tr>
</table></div><div></div></div></div></div><div class="t-navbar-sep"> </div><div class="t-navbar-head"><a href="../language.html" title="cpp/language">C++ 语言</a></div><div class="t-navbar-sep"> </div><div class="t-navbar-head"><strong class="selflink">语句</strong><div class="t-navbar-menu"><div><div style="display:inline-block">
<div><table class="t-nv-begin" cellpadding="0" style="line-height:1.1em">
<tr class="t-nv-h2"><td colspan="5"> 标号 </td></tr>
<tr class="t-nv"><td colspan="5"> <strong class="selflink"> <span class="t-spar">label</span> : <span class="t-spar">statement</span></strong></td></tr>
<tr class="t-nv-h2"><td colspan="5"> 表达式语句 </td></tr>
<tr class="t-nv"><td colspan="5"> <strong class="selflink"> <span class="t-spar">expression</span> ; </strong></td></tr>
<tr class="t-nv-h2"><td colspan="5"> 复合语句 </td></tr>
<tr class="t-nv"><td colspan="5"> <strong class="selflink"> { <span class="t-spar">statement</span>... } </strong></td></tr>
<tr class="t-nv-h2"><td colspan="5"> 选择语句 </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="if.html" title="cpp/language/if"> if</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="switch.html" title="cpp/language/switch"> switch</a></td></tr>
<tr class="t-nv-h2"><td colspan="5"> 循环语句 </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="while.html" title="cpp/language/while"> while</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="do.html" title="cpp/language/do"> do-while</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="for.html" title="cpp/language/for"> for</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="range-for.html" title="cpp/language/range-for">范围 for</a><span class="t-mark-rev t-since-cxx11">(C++11)</span></td></tr>
<tr class="t-nv-h2"><td colspan="5"> 跳转语句 </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="break.html" title="cpp/language/break"> break</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="continue.html" title="cpp/language/continue"> continue</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="return.html" title="cpp/language/return"> return</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="goto.html" title="cpp/language/goto"> goto</a></td></tr>
<tr class="t-nv-h2"><td colspan="5"> 声明语句 </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="declarations.html" title="cpp/language/declarations"> <span class="t-spar">declaration</span> ; </a></td></tr>
<tr class="t-nv-h2"><td colspan="5"> try 块 </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="try_catch.html" title="cpp/language/try catch"> try <span class="t-spar">compound-statement</span> <span class="t-spar">handler-sequence</span></a></td></tr>
<tr class="t-nv-h2"><td colspan="5"> 事务性内存 </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="transactional_memory.html" title="cpp/language/transactional memory"> <code>synchronized</code>, <code>atomic_commit</code></a> 等<span class="t-mark-rev t-since-tm-ts t-mark-ts">(TM TS)</span></td></tr>
</table></div>
</div><div></div></div></div></div><div class="t-navbar-sep"> </div></div>
<p>语句（statement）是依序执行的 C++ 程序片段。任何函数体都是语句的序列。例如：
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="kw4">int</span> main<span class="br0">(</span><span class="br0">)</span>
<span class="br0">{</span>
    <span class="kw4">int</span> n <span class="sy1">=</span> <span class="nu0">1</span><span class="sy4">;</span>                        <span class="co1">// 声明语句</span>
    n <span class="sy1">=</span> n <span class="sy2">+</span> <span class="nu0">1</span><span class="sy4">;</span>                        <span class="co1">// 表达式语句</span>
    <a href="../io/cout.html"><span class="kw1758">std::<span class="me2">cout</span></span></a> <span class="sy1">&lt;&lt;</span> <span class="st0">"n = "</span> <span class="sy1">&lt;&lt;</span> n <span class="sy1">&lt;&lt;</span> <span class="st0">'<span class="es1">\n</span>'</span><span class="sy4">;</span> <span class="co1">// 表达式语句</span>
    <span class="kw1">return</span> <span class="nu0">0</span><span class="sy4">;</span>                         <span class="co1">// return 语句</span>
<span class="br0">}</span></pre></div></div>
<p>C++ 包含下列类型的语句：
</p>
<div class="t-li1"><span class="t-li">1)</span> 表达式语句（expression statement）；</div>
<div class="t-li1"><span class="t-li">2)</span> 复合语句（compound statement）；</div>
<div class="t-li1"><span class="t-li">3)</span> 选择语句（selection statement）；</div>
<div class="t-li1"><span class="t-li">4)</span> 循环语句（iteration statement）；</div>
<div class="t-li1"><span class="t-li">5)</span> 跳转语句（jump statement）；</div>
<div class="t-li1"><span class="t-li">6)</span> 声明语句（declaration statement）；</div>
<div class="t-li1"><span class="t-li">7)</span> try 块；</div>
<div class="t-li1"><span class="t-li">8)</span> atomic 与 synchronized 块<span class="t-mark-rev t-since-tm-ts t-mark-ts">(TM TS)</span>。</div>
<h3><span class="mw-headline" id=".E6.A0.87.E7.AD.BE">标签</span></h3>
<p>通过在语句自身之前提供后随冒号的标签，任何语句都可<i>带有标签</i>。
</p>
<table class="t-sdsc-begin">

<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr>
<tr class="t-sdsc">
<td> <span class="t-spar">属性</span><span class="t-mark">(可选)</span> <span class="t-spar">标识符</span> <code><b>:</b></code> <span class="t-spar">语句</span>
</td>
<td> (1)
</td>
<td class="t-sdsc-nopad">
</td></tr>



<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr>
<tr class="t-sdsc">
<td> <span class="t-spar">属性</span><span class="t-mark">(可选)</span> <code><b>case</b></code> <span class="t-spar">常量表达式</span> <code><b>:</b></code> <span class="t-spar">语句</span>
</td>
<td> (2)
</td>
<td class="t-sdsc-nopad">
</td></tr>



<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr>
<tr class="t-sdsc">
<td> <span class="t-spar">属性</span><span class="t-mark">(可选)</span> <code><b>default</b></code> <code><b>:</b></code> <span class="t-spar">语句</span>
</td>
<td> (3)
</td>
<td class="t-sdsc-nopad">
</td></tr>



<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr></table>
<div class="t-li1"><span class="t-li">1)</span> <a href="goto.html" title="cpp/language/goto">goto</a> 的目标；</div>
<div class="t-li1"><span class="t-li">2)</span> <a href="switch.html" title="cpp/language/switch">switch</a> 语句中的 case 标签；</div>
<div class="t-li1"><span class="t-li">3)</span> <a href="switch.html" title="cpp/language/switch">switch</a> 语句中的 default 标签。</div>
<p>一条语句可以带有多个标签。
</p>
 <table class="t-rev-begin">
<tr class="t-rev t-since-cxx11"><td>
<p><a href="attributes.html" title="cpp/language/attributes">属性</a>序列 <span class="t-spar">属性</span> 可以紧接标签之前出现（这种情况下它应用到标签），或紧跟在语句自身之后出现，这种情况下它应用到整条语句。
</p>
</td>
<td><span class="t-mark-rev t-since-cxx11">(C++11 起)</span></td></tr>
</table>
<p>声明于函数内并带标识符的标签（且仅有标签）在该函数内、所有内嵌块、其自身声明的前后每处都在作用域内。
</p><p>一个函数中的两个标签不得拥有同一标识符。
</p><p><a href="unqualified_lookup.html" title="cpp/language/unqualified lookup">无限定查找</a>找不到标签：标签能与程序中任何其他实体拥有相同的名字。
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="kw4">void</span> f<span class="br0">(</span><span class="br0">)</span>
<span class="br0">{</span>
   <span class="br0">{</span>
       <span class="kw1">goto</span> label<span class="sy4">;</span> <span class="co1">// label 在作用域内，尽管它在后面才声明</span>
label<span class="sy4">:;</span>
   <span class="br0">}</span>
   <span class="kw1">goto</span> label<span class="sy4">;</span> <span class="co1">// label 忽略块作用域</span>
<span class="br0">}</span>
 
<span class="kw4">void</span> g<span class="br0">(</span><span class="br0">)</span>
<span class="br0">{</span>
    <span class="kw1">goto</span> label<span class="sy4">;</span> <span class="co1">// 错误： label 不在 g() 的作用域内</span>
<span class="br0">}</span></pre></div></div>
<h3><span class="mw-headline" id=".E8.A1.A8.E8.BE.BE.E5.BC.8F.E8.AF.AD.E5.8F.A5">表达式语句</span></h3>
<p>表达式后跟一个分号是一条语句。
</p>
<table class="t-sdsc-begin">

<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr>
<tr class="t-sdsc">
<td> <span class="t-spar">属性</span><span class="t-mark">(可选)</span> <span class="t-spar">表达式</span><span class="t-mark">(可选)</span> <code><b>;</b></code>
</td>
<td> (1)
</td>
<td class="t-sdsc-nopad">
</td></tr>



<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr></table>
<table class="t-par-begin">


<tr class="t-par">
<td>  <span class="t-spar">属性</span>
</td>
<td> -
</td>
<td>  <span class="t-mark-rev t-since-cxx11">(C++11 起)</span> 可选的任意数量<a href="attributes.html" title="cpp/language/attributes">属性</a>的序列
</td></tr>
<tr class="t-par">
<td>  <span class="t-spar">表达式</span>
</td>
<td> -
</td>
<td>  一个<a href="expressions.html" title="cpp/language/expressions">表达式</a>
</td></tr></table>
<p>典型 C++ 程序的大部分语句都是表达式语句，例如赋值和函数调用。
</p><p>没有表达式的表达式语句被称作<i>空语句（null statement）</i>。它通常用来为 <a href="for.html" title="cpp/language/for">for</a> 或 <a href="while.html" title="cpp/language/while">while</a> 循环提供空体。它也可用于在复合语句的末尾引入标签。
</p>
<h3><span class="mw-headline" id=".E5.A4.8D.E5.90.88.E8.AF.AD.E5.8F.A5">复合语句</span></h3>
<p>复合语句或（代码）块是花括号环绕的语句序列。
</p>
<table class="t-sdsc-begin">

<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr>
<tr class="t-sdsc">
<td> <span class="t-spar">属性</span><span class="t-mark">(可选)</span> <code><b>{</b></code> <span class="t-spar">语句...</span><span class="t-mark">(可选)</span> <code><b>} </b></code>
</td>
<td> (1)
</td>
<td class="t-sdsc-nopad">
</td></tr>



<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr></table>
<p>当需要一条语句，但要按顺序执行多条语句时（例如在 <a href="if.html" title="cpp/language/if">if</a> 语句或循环中），可以使用复合语句：
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="kw1">if</span> <span class="br0">(</span>x <span class="sy1">&gt;</span> <span class="nu0">5</span><span class="br0">)</span>          <span class="co1">// if 语句的开始</span>
<span class="br0">{</span>                   <span class="co1">// 块的开始</span>
    <span class="kw4">int</span> n <span class="sy1">=</span> <span class="nu0">1</span><span class="sy4">;</span>      <span class="co1">// 声明语句</span>
    <a href="../io/cout.html"><span class="kw1758">std::<span class="me2">cout</span></span></a> <span class="sy1">&lt;&lt;</span> n<span class="sy4">;</span> <span class="co1">// 表达式语句</span>
<span class="br0">}</span>                   <span class="co1">// 块的结束，if 语句的结束</span></pre></div></div>
<p>每个复合语句都引入其自身的块<a href="scope.html" title="cpp/language/scope">作用域</a>；在块中声明的变量在闭花括号处以逆序销毁：
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="kw4">int</span> main<span class="br0">(</span><span class="br0">)</span>
<span class="br0">{</span>
    <span class="br0">{</span>                                <span class="co1">// 块的开始</span>
        <a href="../io/basic_ofstream.html"><span class="kw1733">std::<span class="me2">ofstream</span></span></a> f<span class="br0">(</span><span class="st0">"test.txt"</span><span class="br0">)</span><span class="sy4">;</span> <span class="co1">// 声明语句</span>
        f <span class="sy1">&lt;&lt;</span> <span class="st0">"abc<span class="es1">\n</span>"</span><span class="sy4">;</span>                <span class="co1">// 表达式语句</span>
    <span class="br0">}</span>                                <span class="co1">// 块结束，刷新并关闭 f</span>
    <a href="../io/basic_ifstream.html"><span class="kw1732">std::<span class="me2">ifstream</span></span></a> f<span class="br0">(</span><span class="st0">"test.txt"</span><span class="br0">)</span><span class="sy4">;</span> 
    <a href="../string/basic_string.html"><span class="kw1230">std::<span class="me2">string</span></span></a> str<span class="sy4">;</span>
    f <span class="sy1">&gt;&gt;</span> str<span class="sy4">;</span> 
<span class="br0">}</span></pre></div></div>
<h3><span class="mw-headline" id=".E9.80.89.E6.8B.A9.E8.AF.AD.E5.8F.A5">选择语句</span></h3>
<p>选择语句在数个控制流中选择一个。
</p>
<table class="t-sdsc-begin">

<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr>
<tr class="t-sdsc">
<td> <span class="t-spar">属性</span><span class="t-mark">(可选)</span> <code><b>if</b></code> <code><b>constexpr</b></code><span class="t-mark">(可选)</span> <code><b>(</b></code> <span class="t-spar">初始化语句</span><span class="t-mark">(可选)</span> <span class="t-spar">条件</span> <code><b>)</b></code> <span class="t-spar">true分支语句</span>
</td>
<td> (1)
</td>
<td class="t-sdsc-nopad">
</td></tr>



<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr>
<tr class="t-sdsc">
<td> <span class="t-spar">属性</span><span class="t-mark">(可选)</span> <code><b>if</b></code> <code><b>constexpr</b></code><span class="t-mark">(可选)</span> <code><b>(</b></code> <span class="t-spar">初始化语句</span><span class="t-mark">(可选)</span> <span class="t-spar">条件</span> <code><b>)</b></code> <span class="t-spar">true分支语句</span> <code><b>else</b></code> <span class="t-spar">false分支语句</span>
</td>
<td> (2)
</td>
<td class="t-sdsc-nopad">
</td></tr>



<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr>
<tr class="t-sdsc">
<td> <span class="t-spar">属性</span><span class="t-mark">(可选)</span> <code><b>switch</b></code> <code><b>(</b></code> <span class="t-spar">初始化语句</span><span class="t-mark">(可选)</span> <span class="t-spar">条件</span> <code><b>)</b></code> <span class="t-spar">语句</span>
</td>
<td> (3)
</td>
<td class="t-sdsc-nopad">
</td></tr>



<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr>
<tr class="t-sdsc">
<td> <span class="t-spar">属性</span><span class="t-mark">(可选)</span> <code><b>if</b></code> <code><b>!</b></code><span class="t-mark">(可选)</span> <code><b>consteval</b></code> <span class="t-spar">复合语句</span>
</td>
<td> (4)
</td>
<td> <span class="t-mark-rev t-since-cxx23">(C++23 起)</span>
</td></tr>



<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr>
<tr class="t-sdsc">
<td> <span class="t-spar">属性</span><span class="t-mark">(可选)</span> <code><b>if</b></code> <code><b>!</b></code><span class="t-mark">(可选)</span> <code><b>consteval</b></code> <span class="t-spar">复合语句</span> <code><b>else</b></code> <span class="t-spar">语句</span>
</td>
<td> (5)
</td>
<td> <span class="t-mark-rev t-since-cxx23">(C++23 起)</span>
</td></tr>


}}

<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr></table>
<div class="t-li1"><span class="t-li">1)</span> <a href="if.html" title="cpp/language/if">if</a> 语句；</div>
<div class="t-li1"><span class="t-li">2)</span> 带 else 子句的 <a href="if.html" title="cpp/language/if">if</a> 语句；</div>
<div class="t-li1"><span class="t-li">3)</span> <a href="switch.html" title="cpp/language/switch">switch</a> 语句；</div>
<div class="t-li1"><span class="t-li">4)</span> <a href="if.html#consteval_if" title="cpp/language/if">consteval if</a> 语句；</div>
<div class="t-li1"><span class="t-li">5)</span> 带 else 子句的 <a href="if.html#consteval_if" title="cpp/language/if">consteval if</a> 语句。</div>
<h3><span class="mw-headline" id=".E5.BE.AA.E7.8E.AF.E8.AF.AD.E5.8F.A5">循环语句</span></h3>
<p>循环语句重复执行一些代码。
</p>
<table class="t-sdsc-begin">

<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr>
<tr class="t-sdsc">
<td> <span class="t-spar">属性</span><span class="t-mark">(可选)</span> <code><b>while</b></code> <code><b>(</b></code> <span class="t-spar">条件</span> <code><b>)</b></code> <span class="t-spar">语句</span>
</td>
<td> (1)
</td>
<td class="t-sdsc-nopad">
</td></tr>



<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr>
<tr class="t-sdsc">
<td> <span class="t-spar">属性</span><span class="t-mark">(可选)</span> <code><b>do</b></code> <span class="t-spar">语句</span> <code><b>while</b></code> <code><b>(</b></code> <span class="t-spar">表达式</span> <code><b>)</b></code> <code><b>;</b></code>
</td>
<td> (2)
</td>
<td class="t-sdsc-nopad">
</td></tr>



<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr>
<tr class="t-sdsc">
<td> <span class="t-spar">属性</span><span class="t-mark">(可选)</span> <code><b>for</b></code> <code><b>(</b></code> <span class="t-spar">初始化语句</span> <span class="t-spar">条件</span><span class="t-mark">(可选)</span> <code><b>;</b></code> <span class="t-spar">表达式</span><span class="t-mark">(可选)</span> <code><b>)</b></code> <span class="t-spar">语句</span>
</td>
<td> (3)
</td>
<td class="t-sdsc-nopad">
</td></tr>



<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr>
<tr class="t-sdsc">
<td> <span class="t-spar">属性</span><span class="t-mark">(可选)</span> <code><b>for</b></code> <code><b>(</b></code> <span class="t-spar">for-范围声明</span> <code><b>:</b></code> <span class="t-spar">for-范围初始化器</span> <code><b>)</b></code> <span class="t-spar">语句</span>
</td>
<td> (4)
</td>
<td> <span class="t-mark-rev t-since-cxx11">(C++11 起)</span>
</td></tr>



<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr></table>
<div class="t-li1"><span class="t-li">1)</span> <a href="while.html" title="cpp/language/while">while</a> 循环；</div>
<div class="t-li1"><span class="t-li">2)</span> <a href="do.html" title="cpp/language/do">do-while</a> 循环；</div>
<div class="t-li1"><span class="t-li">3)</span> <a href="for.html" title="cpp/language/for">for</a> 循环；</div>
<div class="t-li1"><span class="t-li">4)</span> <a href="range-for.html" title="cpp/language/range-for">范围 for</a> 循环。</div>
<h3><span class="mw-headline" id=".E8.B7.B3.E8.BD.AC.E8.AF.AD.E5.8F.A5">跳转语句</span></h3>
<p>跳转语句无条件地转移控制流。
</p>
<table class="t-sdsc-begin">

<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr>
<tr class="t-sdsc">
<td> <span class="t-spar">属性</span><span class="t-mark">(可选)</span> <code><b>break</b></code> <code><b>;</b></code>
</td>
<td> (1)
</td>
<td class="t-sdsc-nopad">
</td></tr>



<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr>
<tr class="t-sdsc">
<td> <span class="t-spar">属性</span><span class="t-mark">(可选)</span> <code><b>continue</b></code> <code><b>;</b></code>
</td>
<td> (2)
</td>
<td class="t-sdsc-nopad">
</td></tr>



<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr>
<tr class="t-sdsc">
<td> <span class="t-spar">属性</span><span class="t-mark">(可选)</span> <code><b>return</b></code> <span class="t-spar">表达式</span><span class="t-mark">(可选)</span> <code><b>;</b></code>
</td>
<td> (3)
</td>
<td class="t-sdsc-nopad">
</td></tr>



<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr>
<tr class="t-sdsc">
<td> <span class="t-spar">属性</span><span class="t-mark">(可选)</span> <code><b>return</b></code> <span class="t-spar">花括号初始化器列表</span> <code><b>;</b></code>
</td>
<td> (4)
</td>
<td> <span class="t-mark-rev t-since-cxx11">(C++11 起)</span>
</td></tr>



<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr>
<tr class="t-sdsc">
<td> <span class="t-spar">属性</span><span class="t-mark">(可选)</span> <code><b>goto</b></code> <span class="t-spar">标识符</span> <code><b>;</b></code>
</td>
<td> (5)
</td>
<td class="t-sdsc-nopad">
</td></tr>



<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr></table>
<div class="t-li1"><span class="t-li">1)</span> <a href="break.html" title="cpp/language/break">break</a> 语句；</div>
<div class="t-li1"><span class="t-li">2)</span> <a href="continue.html" title="cpp/language/continue">continue</a> 语句；</div>
<div class="t-li1"><span class="t-li">3)</span> 可带表达式的 <a href="return.html" title="cpp/language/return">return</a> 语句；</div>
<div class="t-li1"><span class="t-li">4)</span> 使用<a href="list_initialization.html" title="cpp/language/list initialization">列表初始化</a>的 <a href="return.html" title="cpp/language/return">return</a> 语句；</div>
<div class="t-li1"><span class="t-li">5)</span> <a href="goto.html" title="cpp/language/goto">goto</a> 语句。</div>
<p>注意：对于所有跳转语句，转移出循环、出块或回到被初始化且具有自动存储期的变量之前，会牵涉到对“ 转移发起点在作用域中而目标点不在，且具有自动存储期 ”的对象的销毁。如果有多个对象被初始化，那么销毁顺序与初始化顺序相反。
</p>
<h3><span class="mw-headline" id=".E5.A3.B0.E6.98.8E.E8.AF.AD.E5.8F.A5">声明语句</span></h3>
<p>声明语句在块中引入一个或多个标识符。
</p>
<table class="t-sdsc-begin">

<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr>
<tr class="t-sdsc">
<td> <span class="t-spar">块声明</span>
</td>
<td> (1)
</td>
<td class="t-sdsc-nopad">
</td></tr>



<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr></table>
<div class="t-li1"><span class="t-li">1)</span> 细节见<a href="declarations.html" title="cpp/language/declarations">声明</a>及<a href="initialization.html" title="cpp/language/initialization">初始化</a>。</div>
<h3><span class="mw-headline" id="try_.E5.9D.97">try 块</span></h3>
<p>try 块提供当执行其他语句时捕获其所抛出的异常的能力。
</p>
<table class="t-sdsc-begin">

<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr>
<tr class="t-sdsc">
<td> <span class="t-spar">属性</span><span class="t-mark">(可选)</span> <code><b>try</b></code> <span class="t-spar">复合语句</span> <span class="t-spar">处理块序列</span>
</td>
<td> (1)
</td>
<td class="t-sdsc-nopad">
</td></tr>



<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr></table>
<div class="t-li1"><span class="t-li">1)</span> 细节见 <a href="try_catch.html" title="cpp/language/try catch">try/catch</a>。</div>
 <table class="t-rev-begin">
<tr class="t-rev t-since-tm_ts"><td>
<h3> <span class="mw-headline" id="atomic_.E4.B8.8E_synchronized_.E5.9D.97">atomic 与 synchronized 块</span></h3>
<p>atomic 与 synchronized 块用来实现<a href="transactional_memory.html" title="cpp/language/transactional memory">事务性内存</a>。
</p>
<table class="t-sdsc-begin">

<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr>
<tr class="t-sdsc">
<td> <code><b>synchronized</b></code> <span class="t-spar">复合语句</span>
</td>
<td> (1)
</td>
<td> <span class="t-mark-rev t-since-tm-ts t-mark-ts">(TM TS)</span>
</td></tr>



<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr>
<tr class="t-sdsc">
<td> <code><b>atomic_noexcept</b></code> <span class="t-spar">复合语句</span>
</td>
<td> (2)
</td>
<td> <span class="t-mark-rev t-since-tm-ts t-mark-ts">(TM TS)</span>
</td></tr>



<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr>
<tr class="t-sdsc">
<td> <code><b>atomic_cancel</b></code> <span class="t-spar">复合语句</span>
</td>
<td> (3)
</td>
<td> <span class="t-mark-rev t-since-tm-ts t-mark-ts">(TM TS)</span>
</td></tr>



<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr>
<tr class="t-sdsc">
<td> <code><b>atomic_commit</b></code> <span class="t-spar">复合语句</span>
</td>
<td> (4)
</td>
<td> <span class="t-mark-rev t-since-tm-ts t-mark-ts">(TM TS)</span>
</td></tr>



<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr></table>
<div class="t-li1"><span class="t-li">1)</span> <a href="transactional_memory.html#synchronized_.E5.9D.97" title="cpp/language/transactional memory">synchronized 块</a>，与所有 synchronized 块在一个全序中执行；</div>
<div class="t-li1"><span class="t-li">2)</span> 在发生异常时中止的 <a href="transactional_memory.html#atomic_.E5.9D.97" title="cpp/language/transactional memory">atomic 块</a>；</div>
<div class="t-li1"><span class="t-li">3)</span> 在发生异常时回滚的 <a href="transactional_memory.html#atomic_.E5.9D.97" title="cpp/language/transactional memory">atomic 块</a>；</div>
<div class="t-li1"><span class="t-li">4)</span> 在发生异常时提交的 <a href="transactional_memory.html#atomic_.E5.9D.97" title="cpp/language/transactional memory">atomic 块</a>。</div>
</td>
<td><span class="t-mark-rev t-since-tm-ts t-mark-ts">(TM TS)</span></td></tr>
</table>
<h3><span class="mw-headline" id=".E5.8F.82.E9.98.85">参阅</span></h3>
<table class="t-dsc-begin">

</table>

<!-- 
NewPP limit report
Preprocessor visited node count: 3535/1000000
Preprocessor generated node count: 8181/1000000
Post‐expand include size: 66752/2097152 bytes
Template argument size: 21407/2097152 bytes
Highest expansion depth: 13/40
Expensive parser function count: 0/100
-->

<!-- Saved in parser cache with key mwiki1-mwiki_zh_:pcache:idhash:8559-0!*!0!!zh!*!zh!* and timestamp 20211022193042 -->
</div>                    <!-- /bodycontent -->
                                        <!-- printfooter -->
                    <div class="printfooter">
                    来自“<a href="https://zh.cppreference.com/mwiki/index.php?title=cpp/language/statements&amp;oldid=72340">https://zh.cppreference.com/mwiki/index.php?title=cpp/language/statements&amp;oldid=72340</a>”                    </div>
                    <!-- /printfooter -->
                                                            <!-- catlinks -->
                    <!-- /catlinks -->
                                                            <div class="visualClear"></div>
                    <!-- debughtml -->
                                        <!-- /debughtml -->
                </div>
                <!-- /bodyContent -->
            </div>
        </div>
        <!-- /content -->
        <!-- footer -->
        <div id="cpp-footer-base" class="noprint">
            <div id="footer">
                        <div id="cpp-navigation">
            <h5>导航</h5>
            <ul><li><a href="https://zh.cppreference.com/w/cpp/language/statements">Online version</a></li><li>Offline version retrieved 2022-01-01 00:37.</li></ul></div>
                        <ul id="footer-info">
                                    <li id="footer-info-lastmod"> 本页面最后修改于2021年9月7日 (星期二) 19:17。</li>
                                    <li id="footer-info-viewcount">此页面已被浏览过16,592次。</li>
                            </ul>
                    </div>
        </div>
        <!-- /footer -->
        <script>if(window.mw){
mw.loader.state({"site":"loading","user":"missing","user.groups":"ready"});
}</script>
<script src="../../../common/skin_scripts.js"></script>
<script>if(window.mw){
mw.loader.load(["mediawiki.action.view.postEdit","mediawiki.user","mediawiki.page.ready","mediawiki.searchSuggest","mediawiki.hidpi","ext.gadget.MathJax","ext.gadget.ColiruCompiler"], null, true);
}</script>
<script src="../../../common/site_scripts.js"></script>
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-2828341-1']);
_gaq.push(['_setDomainName', 'cppreference.com']);
_gaq.push(['_trackPageview']);
</script><!-- Served in 0.713 secs. -->
	</body>
<!-- Cached 20211022193042 -->
</html>